package com.xesj.quartz;

import java.text.ParseException;
import java.util.Date;

import org.quartz.CronExpression;
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.ScheduleBuilder;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.SimpleTrigger;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.spi.TriggerFiredBundle;
import org.springframework.scheduling.quartz.JobDetailAwareTrigger;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;


public class QuartzTest  {
	
	public static void main(String[] args) throws ParseException, InterruptedException {
		/*
		try {
			Scheduler	defaultScheduler = StdSchedulerFactory.getDefaultScheduler();
		
			Trigger trigger = newTrigger().withIdentity("trigger1", "group1") //定义name/group
	                .startNow()//一旦加入scheduler，立即生效
	                .withSchedule(simpleSchedule() //使用SimpleTrigger
	                    .withIntervalInSeconds(1) //每隔一秒执行一次
	                    .repeatForever()) //一直执行
	                .build();
			
			JobDetail job=newJob(JobDemo.class).withIdentity("job1", "group1")
					      .build();
			defaultScheduler.scheduleJob(job, trigger); 
			defaultScheduler.start();
            //运行一段时间后关闭
            Thread.sleep(10000);
            defaultScheduler.shutdown(true);
		} catch (SchedulerException e) {
			
			e.printStackTrace();
		}
		*/
		
		try {
			Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
			JobDetail job = newJob(JobDemo.class).withIdentity("job1", "group1").build();
			Trigger trigger = newTrigger()
					.withIdentity("trigger1", "group1")
					.withSchedule(CronScheduleBuilder.cronSchedule(" 0/2 20 10 * * ? "))
					.build();
			scheduler.scheduleJob(job, trigger);
			scheduler.start();
		} catch (SchedulerException e) {
			
			e.printStackTrace();
		}
		
	}

}
